package HelperFinancial.DataBase;

import java.util.ArrayList;
import java.util.List;

import HelperFinancial.Class.Credito;
import android.content.Context;
import android.database.Cursor;

public class CategoriaAdapter {
	
	private CategoriaHelper BDHelper;
	
	private static CategoriaAdapter instance;

	private CategoriaAdapter(Context context) {
		this.BDHelper = new CategoriaHelper(context);
		if(getAllCategorys().isEmpty()){
        	populateDataBase();
        }
	}
	
	public static CategoriaAdapter getInstance(Context context){
		if(instance == null){
			instance = new CategoriaAdapter(context);
		}
		
		return instance;
	}
	
	public static void destroy() {
		if(instance != null){
			instance.close();
		}
		instance = null;
	}
	
	private void close() {
		BDHelper.close();
	}

	public void addCategory(String name){
		BDHelper.insertCategory(name);
	}
	
	public void removeCategory(String name) {
		BDHelper.removeCategory(name);
	}

	
	public List<String> getAllCategorys() {
		Cursor cr = BDHelper.getAllCategory();
		List<String> allCounts = new ArrayList<String>();
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		
		
		while(cr.moveToNext()){
			allCounts.add(cr.getString(colName));
		}
		return allCounts;
	}
	
	public void populateDataBase(){
		BDHelper.populateDataBase();
	}
	
	
	public String getCreditByName(String name){
		Cursor cr = BDHelper.getCategoryByName(name);
		int colId = cr.getColumnIndex("_id");
		int colName = cr.getColumnIndex("name");
		
		
		cr.moveToFirst();
		String categoria =  cr.getString(colName);
		
		return categoria;
	}
	
	public void removeAll(){
		BDHelper.removeAllCategorys();
	}
	
	


}
